//
//  MSHomePageController.m
//  EPA
//
//  Created by lanou3g on 16/4/15.
//  Copyright © 2016年 myFree. All rights reserved.
//

#import "MSHomePageController.h"
#import "CK_MenuViewController.h"
#import "MSNetWorkRequestManager.h"
#import "MSCollectionViewCell.h"
#import "MSCycleView.h"
#import "MSHomeDetailModel.h"
#import "MSHomeDetailTableViewController.h"
#import "MSRadioViewController.h"
@interface MSHomePageController ()<MenuClickDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
///CollectionView
@property (nonatomic, strong) UICollectionView *collectionView;
///数组属性
@property (nonatomic ,strong) NSMutableArray *dataSourse;
///轮播图属性
@property (nonatomic, strong) MSCycleView *cycleView;
@end

@implementation MSHomePageController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    ///创建轮播图的播放
    self.cycleView = [[MSCycleView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200) interVal:3];
    
    [ CK_MenuViewController getMenuViewController].menuDelegate = self;
    [CK_MenuViewController getMenuViewController].menuArray = [@[@"今日.TODAY",@"话题.TOPICS",@"音乐.MUSIC",@"良品.PRODUCTS",@"订阅.SUBSCRITION",@"设置.SETTING"]mutableCopy];
    ///初始化数据源
    self.dataSourse = [NSMutableArray new];
 [MSNetWorkRequestManager  requestURL:[NSURL URLWithString:READLIST_URL] Method:MS_GET Params:nil Result:^(NSData *data, NSURLResponse *response, NSError *error) {
     if (data) {
           NSDictionary *jsonDic =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *dataDict =jsonDic[@"data"];
         for (NSDictionary *itemDict in dataDict[@"list"] ) {
             MSHomeModel *model = [MSHomeModel new];
             [model setValuesForKeysWithDictionary:itemDict];
             [self.dataSourse addObject:model];
         }
     }
     dispatch_async(dispatch_get_main_queue(), ^{
         [self.collectionView reloadData];
         self.cycleView.imgNameArray = [@[@"zheng_1.jpg",@"zheng_2.jpg",@"zheng_3.jpg",@"zheng_4.jpg",@"zheng_5.jpg",@"zheng_6.jpg"]mutableCopy];
         [self.cycleView addTapBlock:^(NSInteger index) {
             NSLog(@"当前点击第%ld页",(long)index);
             
         }];
     });
     
 }];
    UICollectionViewFlowLayout *layout =[[UICollectionViewFlowLayout alloc]init];

    self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    //
    layout.headerReferenceSize = CGSizeMake(ScreenWidth, 200);
 

    layout.itemSize = CGSizeMake(ScreenWidth *0.3, ScreenWidth*0.3);
    
    self.collectionView.dataSource = self;
    self.collectionView.delegate  = self;
    self.view.backgroundColor = [UIColor greenColor];
    [self.view addSubview:self.collectionView];
   

    ///重用机制
    [self.collectionView registerNib:[UINib nibWithNibName:@"MSCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"Cell"];
    ///注册头
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
    
}
#pragma mark - UICollectViewDelegate
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];
        
        [headerView addSubview:self.cycleView];
        return headerView;
    }
    return nil;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSourse.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MSCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    cell.model = self.dataSourse[indexPath.row];
 
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
    MSHomeDetailTableViewController *detailVC  = [[MSHomeDetailTableViewController alloc]init];
    MSHomeModel *model = self.dataSourse[indexPath.row];
    detailVC.type = model.type;
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//菜单方法 抽屉按钮
- (IBAction)didClickLeftBarButtonItem:(id)sender {
    
    if ([CK_MenuViewController getMenuViewController].isShowing) {
        [[CK_MenuViewController getMenuViewController]hideLeftViewController];
    }else{
        [[CK_MenuViewController getMenuViewController]showLeftViewController];
    }
}
-(void)didClickMenuIndex:(NSInteger) index Title:(NSString *)title {
    //[self.navigationController popToRootViewControllerAnimated:NO];
    switch (index) {
        case 0:
            break;
        case 1: {
            MSRadioViewController *radioVC = [[MSRadioViewController alloc]init];
            [self.navigationController pushViewController:radioVC animated:YES];
        }
            break;
        default:
            break;
    }
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
